import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sptapp/api/interface/user_api.dart';
import 'package:sptapp/api/request/response_data.dart';
import 'package:sptapp/moudle/common_info.dart';

import '../../config/const.dart';
import '../../moudle/message.dart';
import '../../widget/common_widget.dart';

class MinPwdPage extends StatefulWidget {
  final Map arguments;
  const MinPwdPage({super.key, required this.arguments});

  @override
  State<MinPwdPage> createState() => _MinPwdPageState();
}

class _MinPwdPageState extends State<MinPwdPage> {

  String? password;
  String? surePassword;
  String? confirmPassword;
  bool isDisabled = true;
  bool isSureDisabled = true;
  bool isConfirmDisabled = true;

  bool isDone = false;

  TextEditingController mailController = TextEditingController();
  TextEditingController oldPwdController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  TextEditingController surePasswordController = TextEditingController();

  bool isPwdbyy = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    mailController.text = baseUserInfo['email']??'';
  }


  getIsDone(){
    if(password == null || password!.isEmpty || surePassword == null || surePassword!.isEmpty || confirmPassword == null || confirmPassword!.isEmpty ){
      isDone = false;
    }else{
      isDone = true;
    }
    if(mounted){
      setState(() {});
    }
  }

  _register()async{
    if(password!.length<6 || surePassword!.length < 6 || confirmPassword!.length < 6){
      showToast(pwd_length_less.tr);
      return;
    }
    if(confirmPassword! != surePassword!){
      setState(() {
        isPwdbyy = true;
      });
      showToast('Password entered incorrectly Please Re Enter'.tr);
      return;
    }
    Map<String,dynamic> data = {
      'password':password,
      'confirmPassword':confirmPassword
    };
    showLoading();
    ResponseData res = await UserAPI.updatePassWord(data);
    hideLoading();
    if(res.code == 200){
      Navigator.of(context).pop();
      showToast('Success');
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: defaultBlackBgColor,
      appBar: myAppBar(context, 'Password'.tr),
      resizeToAvoidBottomInset: true,
      body: GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Column(
          children: [
            Expanded(child: SingleChildScrollView(
              child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.symmetric(horizontal: 40.w),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    SizedBox(height:40.w,),
                    Container(
                      alignment: Alignment.centerLeft,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(height: 28.w,),
                          Container(
                            child: Text('After setting the login password, you can log in with your account and password.'.tr,style: TextStyle(
                                color: Color.fromRGBO(255, 255, 255, 0.5),
                                fontSize: 24.sp,
                                fontWeight: FontWeight.w400
                            ),),
                          )
                        ],
                      ),
                    ),
                    SizedBox(height: 16.w,),
                    //邮箱
                    Container(
                      width: 670.w,
                      height: 108.w,
                      decoration: BoxDecoration(
                          border: Border(bottom: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1),width: 1.w)),
                      ),
                      child:  Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Container(
                            child: Text('Email',style: TextStyle(
                              color: Colors.white,
                              fontSize: 28.sp,
                              fontWeight: FontWeight.w600
                            ),),
                          ),
                          SizedBox(width: 24.w,),
                          Expanded(child: TextField(
                            //控制器
                            textInputAction: TextInputAction.done,
                            controller: mailController,
                            enabled: false,
                            textAlign: TextAlign.right,
                            maxLines: 1,
                            //是否自动更正
                            autocorrect: true,
                            //是否自动对焦
                            autofocus: false,
                            style: TextStyle(
                                color: Colors.white,
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w400
                            ),
                            inputFormatters: [

                            ],
                            decoration: InputDecoration(
                              contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                              hintText: mail_hintText.tr,
                              hintStyle: TextStyle(
                                  color: Color.fromRGBO(255, 255, 255, 0.5),
                                  fontSize: 28.sp,
                                  fontWeight: FontWeight.w400
                              ),
                              counterText: "",
                              border: OutlineInputBorder(
                                  borderSide: BorderSide.none),
                            ),
                            onChanged: (val){
                            },
                          )),
                        ],
                      ),
                    ),
                    //验证码
                    Container(
                      width: 670.w,
                      height: 108.w,
                      decoration: BoxDecoration(
                        border: Border(bottom: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1),width: 1.w)),
                      ),
                      child: Row(
                        children: [
                          Container(
                            child: Text('Old Password'.tr,style: TextStyle(
                                color: Colors.white,
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w600
                            ),),
                          ),
                          SizedBox(width: 24.w,),
                          Expanded(child: Container(
                            child:  Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Expanded(child: TextField(
                                  //控制器
                                  textInputAction: TextInputAction.done,
                                  controller: oldPwdController,
                                  textAlign: TextAlign.right,
                                  maxLines: 1,
                                  //是否自动更正
                                  autocorrect: true,
                                  //是否自动对焦
                                  autofocus: false,
                                  obscureText: isDisabled,
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 28.sp,
                                      fontWeight: FontWeight.w400
                                  ),
                                  inputFormatters: [
                                  ],
                                  decoration: InputDecoration(
                                    contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                                    hintText: 'Enter password'.tr,
                                    hintStyle: TextStyle(
                                        color: Color.fromRGBO(255, 255, 255, 0.5),
                                        fontSize: 28.sp,
                                        fontWeight: FontWeight.w400
                                    ),
                                    counterText: "",
                                    border: OutlineInputBorder(
                                        borderSide: BorderSide.none),
                                  ),
                                  onChanged: (val){
                                    setState(() {
                                      password = val.trim();
                                    });
                                    getIsDone();
                                  },
                                )),
                                SizedBox(width: 20.w,),
                                GestureDetector(
                                  onTap: (){
                                    setState(() {
                                      isDisabled = !isDisabled;
                                    });
                                  },
                                  child: Container(
                                    child: Image.asset(!isDisabled?'assets/common/input_show.png':'assets/common/input_hide.png',width: 56.w,),
                                  ),
                                )
                              ],
                            ),
                          )),
                        ],
                      ),
                    ),
                    //密码
                    Container(
                      width: 670.w,
                      height: 108.w,
                      decoration: BoxDecoration(
                        border: Border(bottom: BorderSide(color: isPwdbyy? Color(0xffcd0707): Color.fromRGBO(255, 255, 255, 0.1),width: 1.w)),
                      ),
                      child:  Row(
                        children: [
                          Container(
                            child: Text('Password'.tr,style: TextStyle(
                                color: Colors.white,
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w600
                            ),),
                          ),
                          SizedBox(width: 24.w,),
                          Expanded(child: TextField(
                            //控制器
                            controller: passwordController,
                            // maxLength: 11,
                            maxLines: 1,
                            //是否自动更正
                            autocorrect: true,
                            //是否自动对焦
                            autofocus: false,
                            textAlign: TextAlign.right,
                            obscureText: isSureDisabled, //是否是密码
                            style: TextStyle(
                                color: isPwdbyy? Color(0xffcd0707): Colors.white,
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w400
                            ),
                            decoration:  InputDecoration(
                              //                hintText: "请输入用户名",
                              contentPadding: EdgeInsets.zero,
                              hintText: 'Enter password'.tr,
                              hintStyle: TextStyle(
                                  color: Color.fromRGBO(255, 255, 255, 0.5),
                                  fontSize: 28.sp,
                                  fontWeight: FontWeight.w400
                              ),
                              counterText: "",
                              border: const OutlineInputBorder(
                                  borderSide: BorderSide.none),
                            ),
                            onChanged: (val) {
                              setState(() {
                                surePassword = val.trim();
                              });
                              getIsDone();
                            },
                          )),
                          SizedBox(width: 20.w,),
                          GestureDetector(
                            onTap: (){
                              setState(() {
                                isSureDisabled = !isSureDisabled;
                              });
                            },
                            child: Container(
                              child: Image.asset(!isSureDisabled?'assets/common/input_show.png':'assets/common/input_hide.png',width: 56.w,),
                            ),
                          )
                        ],
                      ),
                    ),
                    //确认密码
                    Container(
                      width: 670.w,
                      height: 108.w,
                      decoration: BoxDecoration(
                        border: Border(bottom: BorderSide(color: isPwdbyy? Color(0xffcd0707): Color.fromRGBO(255, 255, 255, 0.1),width: 1.w)),
                      ),
                      child:  Row(
                        children: [
                          Container(
                            child: Text('Confirm Password'.tr,style: TextStyle(
                                color: Colors.white,
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w600
                            ),),
                          ),
                          SizedBox(width: 24.w,),
                          Expanded(child: TextField(
                            //控制器
                            controller: surePasswordController,
                            // maxLength: 11,
                            maxLines: 1,
                            //是否自动更正
                            autocorrect: true,
                            //是否自动对焦
                            autofocus: false,
                            textAlign: TextAlign.right,
                            obscureText: isConfirmDisabled, //是否是密码
                            style: TextStyle(
                                color: isPwdbyy? Color(0xffcd0707): Colors.white,
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w400
                            ),
                            decoration:  InputDecoration(
                              //                hintText: "请输入用户名",
                              contentPadding: EdgeInsets.zero,
                              hintText: 'Enter password'.tr,
                              hintStyle: TextStyle(
                                  color: Color.fromRGBO(255, 255, 255, 0.5),
                                  fontSize: 28.sp,
                                  fontWeight: FontWeight.w400
                              ),
                              counterText: "",
                              border: const OutlineInputBorder(
                                  borderSide: BorderSide.none),
                            ),
                            onChanged: (val) {
                              setState(() {
                                confirmPassword = val.trim();
                              });
                              getIsDone();
                            },
                          )),
                          SizedBox(width: 20.w,),
                          GestureDetector(
                            onTap: (){
                              setState(() {
                                isConfirmDisabled = !isConfirmDisabled;
                              });
                            },
                            child: Container(
                              child: Image.asset(!isConfirmDisabled?'assets/common/input_show.png':'assets/common/input_hide.png',width: 48.w,),
                            ),
                          )
                        ],
                      ),
                    ),
                    SizedBox(height: 64.h,),
                    GestureDetector(
                      onTap: () {
                        getIsDone();
                        if(isDone){
                          _register();
                        }
                      },
                      child: Container(
                        height: 108.w,
                        width: 670.w,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: isDone? themeColor:themeDisableColor,
                            borderRadius: BorderRadius.circular(8.w)
                        ),
                        child: Text('confirm'.tr,style: TextStyle(
                            color: Colors.white,
                            fontSize: 28.sp,
                            fontWeight: FontWeight.w600
                        ),),
                      ),
                    ),
                  ],
                ),
              ),
            )),
            SizedBox(height: ScreenUtil().bottomBarHeight > 0?ScreenUtil().bottomBarHeight:48.w,),
          ],
        ),
      ),
    );
  }

}
